package mo.tags.greedy;

import java.util.Arrays;

public class L826 {

    public int maxProfitAssignment(int[] difficulty, int[] profit, int[] worker) {
        int n = difficulty.length;
        int[][] pairs = new int[n][2];
        for (int i = 0; i < n; i++) {
            pairs[i] = new int[]{profit[i], difficulty[i]};
        }
        Arrays.sort(pairs, (p1, p2) -> {
            if (p1[0] == p2[0]) {
                return p1[1] - p2[1];
            }
            return p2[0] - p1[0];
        });

        Arrays.sort(worker);
        int m = worker.length;

        int res = 0;
        for (int i = 0; i < m; i++) {
            int w = worker[i];
            for (int j = 0; j < n; j++) {
                if (w >= pairs[j][1]) {
                    res += pairs[j][0];
                    break;
                }
            }
        }
        return res;
    }


}
